﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class LiftInfo
    {
        public static bool Add(Model.LiftInfo args)
        {
            string condition = @"INSERT INTO [TableLiftInfo] (
                                        [LiftNum], [LiftCode], [LiftType], 
                                        [LiftGroup], [LiftLine], [LiftLineSerial], 
                                        [LiftCOM], [LiftModel], [LiftActualFloor], 
                                        [LiftSite], [TypeLibNum],[ShortName], 
                                        [Margin], [Angle], [Screen]) 
                                    VALUES (
                                        @LiftNum, @LiftCode, @LiftType, 
                                        @LiftGroup, @LiftLine, @LiftLineSerial, 
                                        @LiftCOM, @LiftModel, @LiftActualFloor, 
                                        @LiftSite, @TypeLibNum, @ShortName, 
                                        @Margin, @Angle, @Screen
                                    )";
            int ret = AccessHelper.ExecuteNonQuery(condition, Param(args));
            if (ret > 0) return true;
            return false;
        }

        public static bool Update(Model.LiftInfo args)
        {
            string condition = @"UPDATE [TableLiftInfo] SET 
                                        [LiftNum]=@LiftNum, [LiftCode]=@LiftCode, [LiftType]= @LiftType, 
                                        [LiftGroup]=@LiftGroup, [LiftLine]= @LiftLine, [LiftLineSerial]= @LiftLineSerial, 
                                        [LiftCOM]=@LiftCOM, [LiftModel]=@LiftModel, [LiftActualFloor]=@LiftActualFloor, 
                                        [LiftSite]=@LiftSite, [TypeLibNum]= @TypeLibNum,[ShortName]= @ShortName, 
                                        [Margin]=@Margin, [Angle]= @Angle, [Screen]=@Screen  
                                WHERE ( ID=@ID)";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(args));
            if (ret > 0) return true;
            return false;
        }

        public static bool Delete(int id)
        {
            return Global.Delete("TableLiftInfo", "ID", id.ToString());
        }

        public static bool Delete()
        {
            return Global.Delete("TableLiftInfo");
        }

        private static OleDbParameter[] Param(Model.LiftInfo args)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@LiftNum", args.LiftNum));
            param.Add(new OleDbParameter("@LiftCode", args.LiftCode));
            param.Add(new OleDbParameter("@LiftType", (int)args.LiftType));
            param.Add(new OleDbParameter("@LiftGroup", args.LiftGroup));
            param.Add(new OleDbParameter("@LiftLine", args.LiftLine));
            param.Add(new OleDbParameter("@LiftLineSerial", args.LiftLineSerial));
            param.Add(new OleDbParameter("@LiftCOM", args.LiftCOM));
            param.Add(new OleDbParameter("@LiftModel", args.LiftModel));
            param.Add(new OleDbParameter("@LiftActualFloor", args.LiftActualFloor));
            param.Add(new OleDbParameter("@LiftSite", args.LiftSite));
            param.Add(new OleDbParameter("@TypeLibNum", args.TypeLibNum));
            param.Add(new OleDbParameter("@ShortName", args.ShortName));
            param.Add(new OleDbParameter("@Margin", args.Margin));
            param.Add(new OleDbParameter("@Angle", args.Angle));
            param.Add(new OleDbParameter("@Screen", args.Screen));
            param.Add(new OleDbParameter("@ID", args.ID));

            return param.ToArray();
        }

        public static bool Exist(Model.LiftInfo args)
        {
            return Global.Exist("TableLiftInfo", "LiftNum", args.LiftNum.ToString());
        }

        public static bool Exist(int id)
        {
            return Global.Exist("TableLiftInfo", "ID", id.ToString());
        }

        public static Model.LiftInfo Select(int liftid)
        {
            DataTable dt = Global.List("TableLiftInfo", " ID=" + liftid);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        public static Model.LiftInfo Select(string field, string value)
        {
            string condition = " " + field + "={0}";
            if (value.isNumeric())
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            DataTable dt = Global.List("TableLiftInfo", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        public static int GetMaxID()
        {
            return Global.GetMaxField("TableLiftInfo", "ID").toInt32();
        }

        public static List<Model.LiftInfo> List(string condition)
        {
            DataTable dt = Global.List("TableLiftInfo", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt);
            }
            return null;
        }

        private static List<Model.LiftInfo> Deserialize(DataTable dt)
        {
            List<Model.LiftInfo> list = null;
            if (null != dt && dt.Rows.Count > 0)
            {
                list = new List<Model.LiftInfo>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(Deserialize(r));
                }
            }
            return list;
        }

        private static Model.LiftInfo Deserialize(DataRow r)
        {
            Model.LiftInfo model = null;
            if (null != r)
            {
                model = new Model.LiftInfo();
                model.ID = r["ID"].ToString().toInt32();
                model.LiftNum = r["LiftNum"].ToString().toInt32();
                model.LiftCode = r["LiftCode"].ToString();
                model.LiftType = (Enum.LiftType)r["LiftType"].ToString().toInt32();
                model.LiftGroup = r["LiftGroup"].ToString().toInt32();
                model.LiftLine = r["LiftLine"].ToString().toInt32();
                model.LiftLineSerial = r["LiftLineSerial"].ToString().toInt32();
                model.LiftCOM = r["LiftCOM"].ToString();
                model.LiftModel = r["LiftModel"].ToString();
                model.LiftActualFloor = r["LiftActualFloor"].ToString();
                model.LiftSite = r["LiftSite"].ToString();
                model.TypeLibNum = r["TypeLibNum"].ToString();
                model.ShortName = r["ShortName"].ToString();
                model.Margin = r["Margin"].ToString();
                model.Angle = r["Angle"].ToString().toInt32();
                model.Screen = r["Screen"].ToString().toInt32();
            }
            return model;
        }


        public static Model.LiftInfo Select(string p)
        {
            throw new System.NotImplementedException();
        }
    }
}
